using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Gameplay.City;

namespace Yoozoo.Gameplay.Hanging
{
    [Serializable]
    public class AirWallGroup
    {
        public string groupName;
        [Header("该组显示的空气墙")]
        public List<GameObject> airWalls;
    }
    
    public class HangingAirWallMgr : MonoBehaviour
    {     
        [SerializeField]
        private AirWallGroup _currentAirWallGroup;
        
        public List<AirWallGroup> airWallGroups;
        private Dictionary<string, AirWallGroup> _airWallGroupMap;

        private void OnEnable()
        {
            // 屏蔽空气墙
            transform.gameObject.SetActive(false);
        }

        public void Init()
        {
            _airWallGroupMap = new Dictionary<string, AirWallGroup>();
            foreach (var airWallGroup in airWallGroups)
            {
                _airWallGroupMap[airWallGroup.groupName] = airWallGroup;
            }

            for (int i = 0; i < transform.childCount; i++)
            {
                transform.GetChild(i).gameObject.SetActive(false);
            }

            _currentAirWallGroup = null;
        }

        public void UnlockWallGroup(string groupName)
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                Init();
            }
#endif

            //HashSet<GameObject> hideWalls = new HashSet<GameObject>();

            if (_currentAirWallGroup!=null && _currentAirWallGroup.airWalls!=null)
            {
                foreach (var airWall in _currentAirWallGroup.airWalls)
                {
                    if (airWall && airWall.activeSelf)
                    {
                        airWall.SetActive(false);
                        //hideWalls.Add(airWall);
                    }
                }
            }
            
            if (_airWallGroupMap.TryGetValue(groupName,out var gp))
            {
                if (gp.airWalls != null) 
                {
                    foreach (var airWall in gp.airWalls)
                    {
                        if (airWall && !airWall.activeSelf)
                        {
                            // airWall.SetActive(true); //TODO: 暂时屏蔽空气墙
                            // if (hideWalls.Contains(airWall))
                            // {
                            //     hideWalls.Remove(airWall);
                            // }
                        }
                    }
                }

                _currentAirWallGroup = gp;
            }

            // 暂时不需要，Timeline有
            // foreach (var hideWall in hideWalls)
            // {
            //     var airWall = hideWall.GetComponent<AirWallDefiner>();
            //     airWall.PlayUnlockEffect();
            // }
            //StartCoroutine(HideWalls(hideWalls));
        }

        private IEnumerator HideWalls(HashSet<GameObject> hideWalls)
        {
            yield return new WaitForSeconds(0.001f);
            foreach (var hideWall in hideWalls)
            {
                hideWall.SetActive(false);
            }
        }

    }
}